<?php
define('IN_TG','photo_detail');
define('IN_JS','article');
define('SCRIPT','photo_detail');
session_start();
require("includes/common.inc.php");
require('header.php');
$tgid = intval($_GET['id']);
//登录状态
if(isset($_GET['id']))
{
	//相册详细
	$sql="select tg_name,tg_sid,tg_date,tg_username,tg_url,tg_readcount,tg_commendcount,tg_content from tg_photo where tg_id={$tgid} limit 1";
	$reres = _query($sql);
	$row = _fetch_array_list($reres);
	//上一页
	$_id = 'id='.$_GET['id'].'&';
	$pre_sql="select tg_id from tg_photo where tg_id<{$tgid} and tg_sid={$row['tg_sid']} order by tg_id desc limit 1";
	$pre_reres = _query($pre_sql);
	$pre_row = _fetch_array_list($pre_reres);
	//下一页
	$next_sql="select tg_id from tg_photo where tg_id>{$tgid} and tg_sid={$row['tg_sid']} order by tg_id asc limit 1";
	$next_reres = _query($next_sql);
	$next_row = _fetch_array_list($next_reres);
	//回复

	_query("update tg_photo set tg_readcount=tg_readcount+1 where tg_id = {$tgid}");
	$sql = "select tu.*,ta.* from tg_photo_commend ta inner join tg_user tu on ta.tg_username = tu.tg_username where  tg_sid={$tgid} order by tg_date asc";
	$size = $_system['article'];
	_page($sql,$size);
	$sql .= " limit $_pagenum,$size";
	$reres = _query($sql);
	$arr=array();
	while($reply_row = _fetch_array_list($reres)){
		$arr[] = $reply_row;
	}
}
if(isset($_GET['action']))
{
	if($_GET['action']=='rephoto'){
		_check_login($_POST['back_url']);
		if($_POST['code'] != $_SESSION['code']){
			$url = "photo_detail.php?id=$tgid";
			_location('验证码错误',$url);
		}elseif(empty($_POST['content'])){
			$url = "photo_detail.php?id=$tgid";
			_location('内容不能为空',$url);
		}else{
			$tg_sid = $_POST['sid'];
			$tg_title = $_POST['title'];
			$tg_content = $_POST['content'];
			$tg_username = $_SESSION['username'];
			$tg_date = date('Y-m-d H:i:s',time());
			$sql = "insert into tg_photo_commend (tg_sid,tg_title,tg_content,tg_date,tg_username) values ({$tg_sid},'{$tg_title}','{$tg_content}','{$tg_date}','{$tg_username}')";
			$res = _query($sql);
			if($res){
				$sql = "update tg_photo set tg_commendcount=tg_commendcount+1 where tg_id = $tgid";
				_query($sql);
				$url = "photo_detail.php?id=$tgid";
				_location('',$url);
			}else{
				$url = "photo_detail.php?id=$tgid";
				_location('数据插入失败',$url);
			}
		}
	}
}
?>
<div id="photo">
	<h2>图片详情</h2>
	<a name="pre"></a><a name="next"></a>
	<dl class="detail">
		<dd class="name"><?php echo $row['tg_name']?></dd>
		<dt>
			<?php if(!empty($pre_row['tg_id'])):?>
			<a href="photo_detail.php?id=<?php echo $pre_row['tg_id'];?>#pre">上一页</a>
			<?php endif;?>
			<img src="<?php echo $row['tg_url']?>" />
			<?php if(!empty($next_row['tg_id'])):?>
			<a href="photo_detail.php?id=<?php echo $next_row['tg_id'];?>#next">下一页</a>
			<?php endif;?>
		</dt>
		<dd>[<a href="photo_show.php?id=<?php echo $row['tg_sid']?>">返回列表</a>]</dd>
		<dd>浏览量(<strong><?php echo $row['tg_readcount']?></strong>) 评论量(<strong><?php echo $row['tg_commendcount']?></strong>) 发表于：<?php echo $row['tg_date']?> 上传者：<?php echo $row['tg_username']?></dd>
		<dd>简介：<?php echo $row['tg_content']?></dd>
	</dl>
	<p class="line"></p>
	<div id="page_num"><?php _paging(1);?></div>
	<?php for($i=0;$i<count($arr);$i++):?>
		<div class="re">
			<dl>
				<dd class="user">
					<?php
					if($i+$_pagenum==0){
						echo $arr[$i]['tg_username'].'(沙发)('.$arr[$i]['tg_sex'].')';
					}else{
						echo $arr[$i]['tg_username'].'('.$arr[$i]['tg_sex'].')';
					}
					?>

				</dd>
				<dt><img src="<?php echo $arr[$i]['tg_face']?>" alt="<?php echo $arr[$i]['tg_username']?>" /></dt>
				<dd class="message"><a href="javascript:;" name="message" title="<?php echo $arr[$i]['tg_id']?>">发消息</a></dd>
				<dd class="friend"><a href="javascript:;" name="friend" title="<?php echo $arr[$i]['tg_id']?>">加为好友</a></dd>
				<dd class="guest">写留言</dd>
				<dd class="flower"><a href="javascript:;" name="flower" title="<?php echo $arr[$i]['tg_id']?>">给他送花</a></dd>
				<dd class="email">邮件：<a href="mailto:<?php echo $arr[$i]['tg_email']?>"><?php echo $arr[$i]['tg_email']?></a></dd>
				<dd class="url">网址：<a href="<?php echo $arr[$i]['tg_url']?>" target="_blank"><?php echo $arr[$i]['tg_url']?></a></dd>
			</dl>
			<div class="content">
				<div class="user">
					<span><?php echo $i+1+$_pagenum;?>#</span><?php echo $arr[$i]['tg_username']?> | 发表于：<?php echo $arr[$i]['tg_date']?>			</div>
				<h3>主题：<?php echo $arr[$i]['tg_title']?><span>[<a href="#ree" name="re" title="回复<?php echo $i+1+$_pagenum;?>楼的<?php echo $arr[$i]['tg_username']?>">回复</a>]</span></h3>
				<div class="detail">
					<?php echo _ubb($arr[$i]['tg_content'])?>
					<p class="autograph"><?php echo _ubb($arr[$i]['tg_autograph'])?></p>			</div>
			</div>
		</div>
		<p class="line"></p>
	<?php endfor;?>
	<form method="post" action="?action=rephoto&id=<?php echo $tgid;?>">
		<input type="hidden" name="back_url" value="<?php echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];?>"/>
		<input type="hidden" name="sid" value="<?php echo $tgid;?>" />
		<dl class="rephoto">
			<dd>标　　题：<input type="text" name="title" class="text" value="RE:<?php echo $row['tg_name']?>" /> (*必填，2-40位)</dd>
			<dd id="q">贴　　图：　<a href="javascript:;">Q图系列[1]</a>　 <a href="javascript:;">Q图系列[2]</a>　 <a href="javascript:;">Q图系列[3]</a></dd>
			<dd>
				<?php require("includes/ubb.inc.php");?>
				<textarea name="content" rows="9"></textarea>
			</dd>
			<dd>
			验 证 码：
			<input type="text" name="code" class="text yzm"  /> <img src="code.php" id="code" /> 
			<input type="submit" class="submit" value="发表帖子" /></dd>
		</dl>
	</form>
</div>
<?php
require('includes/footer.inc.php');
?>
